package com.fastdfs.proto.storage.internal;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import com.fastdfs.proto.FdfsResponse;
import com.fastdfs.proto.storage.DownloadCallback;
import com.fastdfs.proto.storage.FdfsInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件下载结果
 * 
 * @author tobato
 * @param <T>
 *
 */
public class StorageDownloadResponse<T> extends FdfsResponse<T> {

    Logger log= LoggerFactory.getLogger(StorageDownloadResponse.class);

    private DownloadCallback<T> callback;

    public StorageDownloadResponse(DownloadCallback<T> callback) {
        super();
        this.callback = callback;
    }

    /**
     * 解析反馈内容
     */
    @Override
    public T decodeContent(InputStream in, Charset charset) throws IOException {
        // 解析报文内容
        FdfsInputStream input = new FdfsInputStream(in, getContentLength());
        return callback.recv(input);

//        byte[] buff = new byte[4 * 1024];
//        long remainBytes = getContentLength();
//        int bytes;
//        int result;
//
//        while (remainBytes > 0)
//        {
//            if ((bytes=in.read(buff, 0, remainBytes > buff.length ? buff.length : (int)remainBytes)) < 0)
//            {
//                throw new IOException("recv package size " + (getContentLength() - remainBytes) + " != " + bytes);
//            }
//
//              callback.recv(getContentLength(), buff, bytes);
//              remainBytes -= bytes;
//
//            log.debug("totalBytes=" + (getContentLength() - remainBytes));
//        }

        // return callback.recv(getContentLength(), buff, bytes);;


    }

}
